package config

import (
	"time"
)

// DatabaseConfig 数据库配置
type DatabaseConfig struct {
	Driver          string
	DSN             string
	MaxOpenConns    int
	MaxIdleConns    int
	ConnMaxLifetime time.Duration
}

// ServerConfig 服务器配置
type ServerConfig struct {
	ListenAddr       string
	ServerId         uint32
	Responsibilities []string
	MaxConnections   uint32
}

// EtcdConfig etcd配置
type EtcdConfig struct {
	Endpoints   []string
	DialTimeout time.Duration
}

// Config 全局配置
type Config struct {
	Database *DatabaseConfig
	Server   *ServerConfig
	Etcd     *EtcdConfig
}

var globalConfig *Config

// Init 初始化配置
func Init() {
	globalConfig = &Config{
		Database: &DatabaseConfig{
			Driver:          "mysql",
			DSN:             "root:123456@tcp(127.0.0.1:3306)/hero_story",
			MaxOpenConns:    128,
			MaxIdleConns:    16,
			ConnMaxLifetime: 2 * time.Minute,
		},
		Server: &ServerConfig{
			ListenAddr:       "127.0.0.1:123456",
			ServerId:         1001,
			Responsibilities: []string{"LOGIN", "GAME"},
			MaxConnections:   1000,
		},
		Etcd: &EtcdConfig{
			Endpoints:   []string{"127.0.0.1:2379"},
			DialTimeout: 5 * time.Second,
		},
	}
}

// GetConfig 获取全局配置
func GetConfig() *Config {
	return globalConfig
}

// GetDatabaseConfig 获取数据库配置
func GetDatabaseConfig() *DatabaseConfig {
	return globalConfig.Database
}

// GetServerConfig 获取服务器配置
func GetServerConfig() *ServerConfig {
	return globalConfig.Server
}

// GetEtcdConfig 获取etcd配置
func GetEtcdConfig() *EtcdConfig {
	return globalConfig.Etcd
}
